//
//  User.m
//  ProjectB
//
//  Created by lanou on 16/5/5.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "User.h"

@implementation User

// Insert code here to add functionality to your managed object subclass

- (NSInteger)getAge {
    return [[self getLifeTime] objectAtIndex:0].integerValue;
}

- (NSArray<NSString *> *)getLifeTime {
    NSTimeInterval interval = -[self.birthday timeIntervalSinceNow];
    
    NSInteger minutes = (NSInteger)interval / 60;
    NSInteger hours = minutes / 60;
    NSInteger days = hours / 24;
    NSInteger weeks = days / 7;
    NSInteger months = (NSInteger)(days / 30.5);
    NSInteger years = days / 365.25;
    NSTimeInterval yearSecond = 365.25 * 24 * 60 * 60;
    double age = interval / yearSecond;
    
    NSString *minutesStr = [NSString stringWithFormat:@"%ld", minutes];
    NSString *hoursStr = [NSString stringWithFormat:@"%ld", hours];
    NSString *daysStr = [NSString stringWithFormat:@"%ld", days];
    NSString *weeksStr = [NSString stringWithFormat:@"%ld", weeks];
    NSString *monthsStr = [NSString stringWithFormat:@"%ld", months];
    NSString *yearsStr = [NSString stringWithFormat:@"%ld", years];
    NSString *ageStr = nil;
    if (age < 10) {
        ageStr = [NSString stringWithFormat:@"0%.8f", age];
    } else {
        ageStr = [NSString stringWithFormat:@"%.8f", age];
    }
    
    NSArray *returnValue = [[NSArray alloc] initWithObjects:yearsStr, monthsStr, weeksStr, daysStr, hoursStr, minutesStr, ageStr, nil];
    return returnValue;
}

- (NSArray<NSString *> *)getDeadTime {
    NSTimeInterval currentSeconds = -[self.birthday timeIntervalSinceNow];
    
    NSInteger deadMinutes = currentSeconds / (self.maxAge.doubleValue * 365 * 60);
    NSInteger deadMinute = deadMinutes % 60;
    NSInteger deadHour = deadMinutes / 60;
    
    NSInteger minutes = (NSInteger)currentSeconds / 60;
    NSInteger hours = minutes / 60;
    NSInteger days = hours / 24;
    NSInteger years = days / 365.25;

    NSInteger eatNumber = (self.maxAge.integerValue * 365.25 - days) * 3;
    NSInteger sleepNumber = eatNumber / 3;
    NSInteger weekendNumber = sleepNumber / 7;
    NSInteger valentineNumber = self.maxAge.integerValue - years;
    
    NSString *deadMinuteStr = [NSString stringWithFormat:@"%ld", deadMinute];
    NSString *deadHourStr = [NSString stringWithFormat:@"%ld", deadHour];
    NSString *eatStr = [NSString stringWithFormat:@"%ld", eatNumber];
    NSString *sleepStr = [NSString stringWithFormat:@"%ld", sleepNumber];
    NSString *weekendStr = [NSString stringWithFormat:@"%ld", weekendNumber];
    NSString *valentineStr = [NSString stringWithFormat:@"%ld", valentineNumber];
    
    NSArray *returnValue = [[NSArray alloc] initWithObjects:eatStr, sleepStr, weekendStr, valentineStr, deadHourStr, deadMinuteStr, nil];
    return returnValue;
}

@end
